package com.FundamentalDemos.ObjectOriented;

public class 封装思想与类成员可见性修饰符 {

    public static void main(String[] args) {
        Student1 stu = new Student1();
//        System.out.println(stu.score);//报错!因为Student1中的score成员是private私有成员,因此只有Student1类内部可见
//        stu.isPass();//报错!私有函数外界不可见
        System.out.println(stu.num);//不报错!因为num成员是public公共成员
        stu.whatIsYourNum();//不报错!公共函数外界可见


        System.out.println("----------------------------");
        System.out.println("stu.score = " + stu.getScore());
        stu.setScore(101);
        System.out.println("stu.score = " + stu.getScore());
        stu.setScore(100);
        System.out.println("stu.score = " + stu.getScore());

    }

}

class Student1 {
    private double score;
    public double num;

    private void isPass() {
        System.out.println(this.score >= 60 ? "及格" : "不及格");
    }

    public void whatIsYourNum() {
        System.out.println("num = " + this.num);
    }

    //合理隐藏,合理暴露!
    //给成员变量设置可见性为private后,避免被不当访问,可以通过使用Getter()/Setter()来给成员变量赋值/获取成员变量,在Getter()/Setter()中可以进行一定的逻辑判断,从而避免非法数据
    public void setScore(double score) {
        //进行逻辑判断,避免非法输入
        if (score < 0 || score > 100) {
            System.out.println("输入非法!赋值失败!");
            return;
        }
        this.score = score;
        System.out.println("赋值成功!");

    }

    public double getScore() {
        return score;
    }

}

